using System;
using System.Data;

using System.Configuration;

using System.Data.OleDb;
using Entity;

namespace SalesVMT
{
    public class ReceiptDetailsDB
    {
        #region PUBLIC
        public static DataSet GetAll()
        {
            DataSet retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();

            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_ReceiptDetails_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetList(out int ItemCount, string keyWord, int currentPage, int pageSize)
        {

            DataSet retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_ReceiptDetails_GetList", dbConn);
            dbCmd.Parameters.AddWithValue("@KeyWord", keyWord);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@RPP", pageSize);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }
        public static DataSet GetAllByReceipt(int ReceiptID)
        {
            DataSet retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();

            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_ReceiptDetails_GetAllByReceipt", dbConn);
            dbCmd.Parameters.AddWithValue("@Receipt_ID", ReceiptID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Delete(int _receipt_ID)
        {
            bool retVal = false;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_ReceiptDetails_Delete", dbConn);
            dbCmd.Parameters.AddWithValue("@Receipt_ID", _receipt_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Insert(ReceiptDetailsInfo _ReceiptDetailsInfo)
        {
            int retVal = 0;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_ReceiptDetails_Insert", dbConn);
            dbCmd.Parameters.AddWithValue("@ReceiptDetails_ItemID", _ReceiptDetailsInfo.ReceiptDetails_ItemID);
            dbCmd.Parameters.AddWithValue("@ReceiptDetails_ReceiptID", _ReceiptDetailsInfo.ReceiptDetails_ReceiptID);
            dbCmd.Parameters.AddWithValue("@ReceiptDetails_Quantity", _ReceiptDetailsInfo.ReceiptDetails_Quantity);
            dbCmd.Parameters.AddWithValue("@ReceiptDetails_Price", _ReceiptDetailsInfo.ReceiptDetails_Price);
            dbCmd.Parameters.AddWithValue("@ReceiptDetails_DownPrice", _ReceiptDetailsInfo.ReceiptDetails_DownPrice);
            dbCmd.Parameters.AddWithValue("@ReceiptDetails_VAT", _ReceiptDetailsInfo.ReceiptDetails_VAT);
            dbCmd.Parameters.AddWithValue("@ReceiptDetails_Total", _ReceiptDetailsInfo.ReceiptDetails_Total);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static ReceiptDetailsInfo GetInfo(int _ReceiptDetails_ID)
        {
            ReceiptDetailsInfo retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_ReceiptDetails_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@ReceiptDetails_ID", _ReceiptDetails_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            OleDbDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new ReceiptDetailsInfo();
                    retVal.ReceiptDetails_ID = Convert.ToInt32(dr["ReceiptDetails_ID"]);
                    retVal.ReceiptDetails_ItemID = Convert.ToInt32(dr["ReceiptDetails_ItemID"]);
                    retVal.ReceiptDetails_ReceiptID = Convert.ToInt32(dr["ReceiptDetails_ReceiptID"]);
                    retVal.ReceiptDetails_Quantity = Convert.ToInt32(dr["ReceiptDetails_Quantity"]);
                    retVal.ReceiptDetails_Price = Convert.ToDecimal(dr["ReceiptDetails_Price"]);
                    retVal.ReceiptDetails_DownPrice = Convert.ToDecimal(dr["ReceiptDetails_DownPrice"]);
                    retVal.ReceiptDetails_VAT = Convert.ToInt32(dr["ReceiptDetails_VAT"]);
                    retVal.ReceiptDetails_Total = Convert.ToDecimal(dr["ReceiptDetails_Total"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        #endregion
    }
}
